//
//  LoginViewModel.m
//  DataStatistics
//
//  Created by Kang on 16/6/13.
//  Copyright © 2016年 YTYangK. All rights reserved.
//

#import "LoginViewModel.h"
#import "LoginModel.h"
#import "TabBarC.h"
//#import "DeploySDK.h"
#import "GetiPhoneDevice.h"
#import "JPUSH.h"

#define  JSESSIONID @"JSESSIONID"


@interface LoginViewModel ()
@property (assign, nonatomic) BOOL isState; //获取状态
@end

@implementation LoginViewModel

// API职责 --负责发起请求， 不负责返回数据
#pragma mark - 登陆
+ (void) requestWithUrlForLoginIn:(id)obj  andParam:(NSDictionary *)param success:(void(^)(LoginModel * model))success failure:(void(^)(NSString * error))failure{
  
    MDC_INTER_DIC(@"app/login.do", @"NetMethodPOST", @"true");
 __weak typeof(self) weakSelf = self;
    [[UtilToolsClss getUtilTools] addDoLoading];
    
    [NetRequestClss requestWithUrl:[inter objectForKey:@"mdc_Url"] requestWithParameters:param method:(NetMethod)[inter objectForKey:@"isMethod"] returnSuccess:^(id objs, int status, NSString *mag) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        
        NSLog(@"%@",objs);
        
        if ([[objs objectForKey:@"result"] isEqualToString:@"success"]) {
            
            LoginModel *list = [LoginModel mj_objectWithKeyValues:objs];

            [weakSelf obtainHTTPCookie];
            
            if (objs) {
                success(list);
                return;
            }
            
        }else {
            failure([objs objectForKey:@"msg"]);
        }
        
    } returnError:^(NSString *err) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        NSLog(@"%@",err);
        failure(err);
    }];
}


#pragma mark - 验证手机注册情况 -已注册或者，未注册
+ (void)requestWithUrlForRegisteredAccountSign:(NSString *)sign andParam:(NSDictionary *)param success:(void (^)(id model))success failure:(void (^)(NSString *err))failure {
 
    [[UtilToolsClss getUtilTools] addDoLoading];
    [NetRequestClss requestWithUrl:@"/app/checkRegister.do" requestWithParameters:param method:NetMethodPOST returnSuccess:^(id objs, int status, NSString *mag) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        
        NSString *regisStr = [objs objectForKey:@"result"];
        
        if ([regisStr isEqualToString:@"success"]) {  // 未注册
            if ([sign isEqualToString:@"Registered"]) {
                success(regisStr);
            }else {
                failure(NSLocalizedString(@"RvcTip_2", nil));
            }
        }else if([regisStr isEqualToString:@"error"]) {  // 注册
            if ([sign isEqualToString:@"Forget"]) {
                success(regisStr);
            }else {
                failure(NSLocalizedString(@"RvcTip_3", nil));
            }
        }else { // 0
            failure(NSLocalizedString(@"RvcTip_4", nil));
        }
        
        
    } returnError:^(NSString *err) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        failure(err);
    }];
}


#pragma mark - 校验手机短信
+ (void)requestWithUrlForVerification:(id)obj andParam:(NSDictionary *)param success:(void(^)(id model))success failure:(void(^)(NSString *err))failure {
    [[UtilToolsClss getUtilTools] addDoLoading];
    
    [NetRequestClss requestWithUrl:@"/app/checkCode.do" requestWithParameters:param method:NetMethodPOST returnSuccess:^(id objs, int status, NSString *mag) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        
        NSLog(@"校验 ==========  %@",objs);
        
        if ([[objs objectForKey:@"result"] isEqualToString:@"success"]) {
            success([objs objectForKey:@"result"]);
        }else {

            failure([objs objectForKey:@"msg"]);
        }
        
    } returnError:^(NSString *err) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        failure(err);
    }];

}


#pragma  mark - 重新设置密码
+ (void)requestWithUrlForForgetPassword:(id)obj andParam:(NSDictionary *)param success:(void(^)(id model))success failure:(void(^)(NSString *err))failure {
    [[UtilToolsClss getUtilTools] addDoLoading];
    [NetRequestClss requestWithUrl:@"/app/forgetPassword.do" requestWithParameters:param method:NetMethodPOST returnSuccess:^(id objs, int status, NSString *mag) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        
        
        success([objs objectForKey:@"success"]);
      
        
    } returnError:^(NSString *err) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        failure(err);
    }];

}

#pragma mark - 账号注册
+(void)requestWithURlForUserRegister:(id)obj andParam:(NSDictionary *)param success:(void (^)(id model))success failure:(void(^)(NSString *err)) failure {
    
    [[UtilToolsClss getUtilTools] addDoLoading];
    [NetRequestClss requestWithUrl:@"/app/register.do" requestWithParameters:param method:NetMethodGET returnSuccess:^(id objs, int status, NSString *mag) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        
        if ([[objs objectForKey:@"result"] isEqualToString:@"success"]) {
            success([objs objectForKey:@"result"]);
        }else{
            failure([objs objectForKey:@"msg"]);
        }
        
    } returnError:^(NSString *err) {
        [[UtilToolsClss getUtilTools] removeDoLoading];
        failure(err);
    }];

}


// 保存 cooke //ios 11后怀疑被拦截无法
+ (void)obtainHTTPCookie {
    
    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
    NSData *cookiesData = [NSKeyedArchiver archivedDataWithRootObject:[cookieStorage cookies]];
    
    [NSUD setValue:cookiesData forKey:@"allCookie"];
    
    
    
    for (NSHTTPCookie *cookie in [cookieStorage cookies]) {
        
        NSLog(@"打印cookie：%@",cookie.name);

        if ([cookie.name isEqualToString:JSESSIONID]) {
            
          NSLog(@"打印cookie：%@",cookie.value);
            [NSUD setObject:cookie.value forKey:@"token"];
            break;
        }
    }
}




/** 跳转页面 */
+ (void)loginWithViewController:(UIViewController *)viewController {
       
        [[NSUserDefaults standardUserDefaults]setObject:@"YES" forKey:@"isAutomaticLogin"];
        
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        TabBarC *tab = [storyboard instantiateInitialViewController];
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [UIApplication sharedApplication].statusBarHidden = NO;
        window.rootViewController = tab;
    
//        if (![[GetiPhoneDevice getCurrentDeviceModel] isEqualToString:@"iPhone Simulator"]) {
            [JPUSH sharedJPUSH].sonWindow = window;
            [JPUSH sharedJPUSH].tabBarC    =  tab; //(TabBarC *)self.tabBarController;
            [[JPUSH sharedJPUSH] albbBindChannel]; //这个是绑定推送的账号，问题是如果注册失败会重复执行
//        }
}







@end
